using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DTN.Core.Common
{
    public class Pager : Control
    {
        private int curPage = 1;
        private int startPage = 1;
        private int maxViewPage = 4;
        private int curPageManual = -1;
        public int CurrentPageManual
        {
            get { return this.curPageManual; }
            set { this.curPageManual = value; }
        }
        public int MaxViewPage
        {
            get { return this.maxViewPage; }
            set { this.maxViewPage = value; }
        }
        private int totalPage = 1;
        public int TotalPage
        {
            get { return this.totalPage; }
            set { this.totalPage = value; }
        }

        private string onClickTemplate;
        public string OnClickTemplate
        {
            get { return this.onClickTemplate; }
            set { this.onClickTemplate = value; }
        }
        private string onClickNavigate = "";
        public string OnClickNavigate
        {
            get { return this.onClickNavigate; }
            set { this.onClickNavigate = value; }
        }
        private const int nextpagechangestep = 2;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        
            if (this.Page.Request.Params["page"] != null)
                curPage = Convert.ToInt32(this.Page.Request.Params["page"]);
            if (curPageManual != -1)
                curPage = curPageManual;

            if (curPage % maxViewPage != 0)
                startPage = (curPage / maxViewPage) * maxViewPage + 1;
            else
                startPage = (curPage / maxViewPage - 1) * maxViewPage + 1;

            
        }
        protected override void Render(HtmlTextWriter output)
        {
            if ((onClickTemplate == "" || string.IsNullOrEmpty(onClickTemplate)) && (onClickNavigate == "" || string.IsNullOrEmpty(onClickNavigate)))
                return;


            StringBuilder html = new StringBuilder();

            html.Append("<div class='PageNum aright'>");
            html.Append("");
            int backPage = 1;
            if (startPage - maxViewPage >= 0)
                backPage = startPage - maxViewPage;
            int nextPage = startPage;
            if (startPage + maxViewPage <= totalPage)
                nextPage = startPage + maxViewPage;

            if (onClickNavigate == "")
                html.Append("<a href=\"javascript:void(0)\"  onclick=\"" + string.Format(OnClickTemplate, backPage).Replace("[", "{").Replace("]", "}") + "\"></a>&nbsp;");
            else html.Append("<a href=\"" + string.Format(OnClickNavigate, backPage) + "\" onclick=\"" + string.Format(OnClickTemplate, backPage).Replace("[", "{").Replace("]", "}") + ";return false;\">...</a>&nbsp;");
           
            for (int i = 0; i < MaxViewPage; i++)
            {
                int page = startPage + i;
                if (page > TotalPage) break;
                if (curPage == page)
                {
                    if (onClickNavigate == "")
                        html.Append("<strong><a class=\"pagenumcurrent\" href=\"javascript:void(0)\" onclick=\"" + string.Format(OnClickTemplate, startPage + i).Replace("[", "{").Replace("]", "}") + "\"> " + (page) + "</a></strong>");
                    else html.Append("<strong><a class=\"pagenumcurrent\" href=\"" + string.Format(OnClickNavigate, startPage + i) + "\" onclick=\"" + string.Format(OnClickTemplate, startPage + i).Replace("[", "{").Replace("]", "}") + ";return false;\"> " + (page) + "</a></strong>");
                }
                else
                {
                    if (onClickNavigate == "")
                        html.Append("<a href=\"javascript:void(0)\" class=\"numpaging albumimage\"  onclick=\"" + string.Format(OnClickTemplate, startPage + i).Replace("[", "{").Replace("]", "}") + "\"> " + (page) + "</a>");
                    else html.Append("<a href=\"" + string.Format(OnClickNavigate, startPage + i) + "\" onclick=\"" + string.Format(OnClickTemplate, startPage + i).Replace("[", "{").Replace("]", "}") + ";return false;\"> " + (page) + "</a>");
                }
            }
            if (onClickNavigate == "")
                html.Append("<a href=\"javascript:void(0)\" onclick=\"" + string.Format(OnClickTemplate, nextPage).Replace("[", "{").Replace("]", "}") + "\"></a>&nbsp;");
            else html.Append("<a href=\"" + string.Format(OnClickNavigate, nextPage) + "\" onclick=\"" + string.Format(OnClickTemplate, nextPage).Replace("[", "{").Replace("]", "}") + ";return false;\">...</a>&nbsp;");
            html.Append("</div>");
            output.Write(html);
        }
    }
}
